package Q4;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {

    public static User getUser(){
        Scanner input=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String userName=input.next();
        System.out.println("请输入密码：");
        String password=input.next();
        //封装
        return new User(userName,password);
    }

    public static void main(String[] args) throws IOException {
        Socket client= null;
        ObjectOutputStream oos= null;
        DataInputStream dis= null;
        try {
            //创建Socket对象，用于连接服务器
            client = new Socket("localhost",8888);
            //获取输出流（对象流）
            oos = new ObjectOutputStream(client.getOutputStream());
            //创建User对象
            User  u=getUser();
            //user对象发送到服务器
            oos.writeObject(u);
            //获取输入流
            dis = new DataInputStream(client.getInputStream());
            System.out.println(dis.readUTF());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            try {
                if(dis!=null){
                    dis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(oos!=null){
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(client!=null){
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
